package cn.ecut.controller;

import cn.ecut.model.Dormitory;
import cn.ecut.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Controller
/*@RequestMapping("/dormitory")*/
public class DormitoryController {
    @Autowired
    DormitoryService dormitoryService;

    //申请宿舍
    @RequestMapping("/apply")
    public String apply(@RequestParam("date") String date,
                        @RequestParam("expireDate") String expireDate,
                        @RequestParam("userId") String userId,
                        @RequestParam("sex") String sex,
                        @RequestParam("applicant") String applicant,
                        @RequestParam(value = "department", required = false) String department,
                        @RequestParam("phoneNumber") String phoneNumber,
                        @RequestParam("workPhone") String workPhone,
                        @RequestParam("education") String education,
                        @RequestParam("IDCard") String IDCard,
                        @RequestParam("province") String province,
                        @RequestParam("city") String city,
                        @RequestParam("district") String district,
                        @RequestParam(value = "address", required = false) String address,
                        @RequestParam("reason") String reason,
                        @RequestParam(value = "remarks", required = false) String remarks,
                        @RequestParam("building") String building,
                        @RequestParam("floor") String floor,
                        @RequestParam("room") String room) throws ParseException {
        Dormitory dormitory = new Dormitory();
        //单据编号，随机生成
        String number = String.valueOf(100000 + new Random().nextInt(899999));
        dormitory.setNumber(number);
        //申请日期
        dormitory.setDate(date);
        //结束日期
        dormitory.setExpireDate(expireDate);
        dormitory.setUserId(userId);
        dormitory.setSex(sex);
        dormitory.setApplicant(applicant);
        dormitory.setDepartment(department);
        //申请的初始状态为“申请中”
        dormitory.setState("申请中");
        dormitory.setPhoneNumber(phoneNumber);
        dormitory.setWorkPhone(workPhone);
        dormitory.setEducation(education);
        //入行时间
        dormitory.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        dormitory.setIDCard(IDCard);
        dormitory.setAddress(province + city + district + address);
        dormitory.setReason(reason);
        dormitory.setRemarks(remarks);
        dormitory.setBuilding(Integer.parseInt(building));
        dormitory.setFloor(Integer.parseInt(floor));
        dormitory.setRoom(Integer.parseInt(room));
        dormitoryService.apply(dormitory);
        return "user/applySuccess";
    }

    @RequestMapping("/updataState")
    public String updateStatus(@RequestParam("contract_num") String contract_num){
        dormitoryService.updateStatus(contract_num);
        return "admin/getApplyList";
    }
}
